package kr.moiba.request.web;

import java.io.File;
import java.net.URLEncoder;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import kr.moiba.commonCd.service.CommonCdSvc;
import kr.moiba.vertification.domain.Vertification;
import kr.moiba.vertification.service.VertificationSvc;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.support.MessageSourceAccessor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.sun.org.apache.xerces.internal.impl.dv.util.Base64;

/**
 * @author 이 영호(Youngho Lee)
 */

@Controller
@RequestMapping("/request")
public class RequestCtrl {
	
	private final Logger logger = Logger.getLogger(getClass());
	
	@Autowired
	private MessageSourceAccessor messages;
	
	@Autowired
	private VertificationSvc vertificationSvc;
	
	@Autowired
	private CommonCdSvc commonCdSvc;
		
	@RequestMapping(value={"/applet.do"})
	public Map<String, Object> applet(HttpServletRequest request) throws Exception {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		
		Integer idx = ServletRequestUtils.getIntParameter(request, "idx");
		if(idx != null){
			resultMap.put("vertification", vertificationSvc.findby(idx));	
		}
		
		//공통코드
		//테스트 단말
		Map<String, Object> condition = new HashMap<String, Object>();
		condition.put("hIdx", 1);
		resultMap.put("testDeviceList", commonCdSvc.findbyListData(condition));
		
		return resultMap;
	}

	@RequestMapping(value={"/pay.do"})
	public Map<String, Object> pay(HttpServletRequest request, HttpSession session) throws Exception {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		
		Integer idx = ServletRequestUtils.getIntParameter(request, "idx");
		if(idx != null){
			resultMap.put("vertification", vertificationSvc.findby(idx));	
		}
		
		//공통코드
		//테스트 단말
		Map<String, Object> condition = new HashMap<String, Object>();
		condition.put("hIdx", 2);
		resultMap.put("testDeviceList", commonCdSvc.findbyListData(condition));
		
		return resultMap;
	}
	
	@RequestMapping(value={"/submit.do"})
	public ModelAndView Submit(HttpServletRequest request, HttpSession session, Vertification vertification) throws Exception {
		String fAct = ServletRequestUtils.getStringParameter(request, "fAct", "add");
		String ip = request.getRemoteAddr();
		String userId = (String)session.getAttribute("userId");
		
		long time = Calendar.getInstance().getTimeInMillis();
		String prefixNm = Base64.encode((URLEncoder.encode(userId)+Long.toString(time)).getBytes());
		
		//콘텐츠 소개 파일 업로드
		MultipartFile capFile = vertification.getCapFile();
		if(capFile != null && ! capFile.isEmpty()){
			String fileNm = "."+(capFile.getOriginalFilename()).split("\\.")[1];

			//업로드 경로
			String uploadDir = messages.getMessage("file.capFile.uploadDir") + prefixNm;
			
			//viewUrl
			String viewUrl = messages.getMessage("file.capFile.viewUrl") + prefixNm;

			File file = new File(uploadDir+fileNm);
			String url = viewUrl+fileNm;
			vertification.setCap(url);

			capFile.transferTo(file);
		}
		
		if(fAct.equals("add")){
			vertification.setCreatedIp(ip);
			vertification.setCreatedId(userId);
			vertification = vertificationSvc.add(vertification);
		}else if(fAct.equals("modify")){
			vertification.setModifiedIp(ip);
			vertification.setModifiedId(userId);
			vertification = vertificationSvc.modify(vertification);
		}
		
		return new ModelAndView("request/result", "vertification", vertification);
	}
}